package com.aiassistant.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 设置模型类，使用单例模式管理应用设置
 */
public class Settings {
    private static final String CONFIG_FILE = "config.properties";
    private static Settings instance;
    private Properties properties;
    
    // 默认设置值
    private static final String DEFAULT_FONT_SIZE = "中";
    private static final String DEFAULT_API_MODEL = "GPT-3.5";
    private static final String DEFAULT_API_KEY = "";
    private static final String DEFAULT_API_URL = "https://api.openai.com/v1/chat/completions";
    private static final String DEFAULT_HISTORY_DAYS = "30天";
    private static final boolean DEFAULT_DARK_MODE = false;
    private static final boolean DEFAULT_AUTO_REPLY = false;
    private static final boolean DEFAULT_SOUND_NOTIFICATION = true;
    private static final String APP_VERSION = "1.0.0";
    
    /**
     * 私有构造函数
     */
    private Settings() {
        properties = new Properties();
        loadSettings();
    }
    
    /**
     * 获取单例实例
     */
    public static synchronized Settings getInstance() {
        if (instance == null) {
            instance = new Settings();
        }
        return instance;
    }
    
    /**
     * 加载设置
     */
    private void loadSettings() {
        File configFile = new File(CONFIG_FILE);
        
        if (configFile.exists()) {
            try (FileInputStream fis = new FileInputStream(configFile)) {
                properties.load(fis);
                System.out.println("成功加载配置文件");
            } catch (IOException e) {
                System.err.println("加载配置文件失败: " + e.getMessage());
                setDefaultSettings();
            }
        } else {
            System.out.println("配置文件不存在，使用默认设置");
            setDefaultSettings();
        }
    }
    
    /**
     * 设置默认值
     */
    private void setDefaultSettings() {
        properties.setProperty("fontSize", DEFAULT_FONT_SIZE);
        properties.setProperty("apiModel", DEFAULT_API_MODEL);
        properties.setProperty("apiKey", DEFAULT_API_KEY);
        properties.setProperty("apiUrl", DEFAULT_API_URL);
        properties.setProperty("historyDays", DEFAULT_HISTORY_DAYS);
        properties.setProperty("darkMode", String.valueOf(DEFAULT_DARK_MODE));
        properties.setProperty("autoReply", String.valueOf(DEFAULT_AUTO_REPLY));
        properties.setProperty("soundNotification", String.valueOf(DEFAULT_SOUND_NOTIFICATION));
    }
    
    /**
     * 重置为默认设置
     */
    public void resetToDefaults() {
        setDefaultSettings();
        saveSettings();
    }
    
    /**
     * 保存设置
     */
    public void saveSettings() {
        try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE)) {
            properties.store(fos, "AI Assistant Configuration");
            System.out.println("成功保存配置");
        } catch (IOException e) {
            System.err.println("保存配置失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取字体大小
     */
    public String getFontSize() {
        return properties.getProperty("fontSize", DEFAULT_FONT_SIZE);
    }
    
    /**
     * 设置字体大小
     */
    public void setFontSize(String fontSize) {
        properties.setProperty("fontSize", fontSize);
    }
    
    /**
     * 获取API模型
     */
    public String getApiModel() {
        return properties.getProperty("apiModel", DEFAULT_API_MODEL);
    }
    
    /**
     * 设置API模型
     */
    public void setApiModel(String apiModel) {
        properties.setProperty("apiModel", apiModel);
    }
    
    /**
     * 获取API密钥
     */
    public String getApiKey() {
        return properties.getProperty("apiKey", DEFAULT_API_KEY);
    }
    
    /**
     * 设置API密钥
     */
    public void setApiKey(String apiKey) {
        properties.setProperty("apiKey", apiKey);
    }
    
    /**
     * 获取API地址
     */
    public String getApiUrl() {
        return properties.getProperty("apiUrl", DEFAULT_API_URL);
    }
    
    /**
     * 设置API地址
     */
    public void setApiUrl(String apiUrl) {
        properties.setProperty("apiUrl", apiUrl);
    }
    
    /**
     * 获取消息历史保存天数
     */
    public String getHistoryDays() {
        return properties.getProperty("historyDays", DEFAULT_HISTORY_DAYS);
    }
    
    /**
     * 设置消息历史保存天数
     */
    public void setHistoryDays(String historyDays) {
        properties.setProperty("historyDays", historyDays);
    }
    
    /**
     * 是否开启暗黑模式
     */
    public boolean isDarkMode() {
        return Boolean.parseBoolean(properties.getProperty("darkMode", String.valueOf(DEFAULT_DARK_MODE)));
    }
    
    /**
     * 设置暗黑模式
     */
    public void setDarkMode(boolean darkMode) {
        properties.setProperty("darkMode", String.valueOf(darkMode));
    }
    
    /**
     * 是否开启自动回复
     */
    public boolean isAutoReply() {
        return Boolean.parseBoolean(properties.getProperty("autoReply", String.valueOf(DEFAULT_AUTO_REPLY)));
    }
    
    /**
     * 设置自动回复
     */
    public void setAutoReply(boolean autoReply) {
        properties.setProperty("autoReply", String.valueOf(autoReply));
    }
    
    /**
     * 是否开启声音提醒
     */
    public boolean isSoundNotification() {
        return Boolean.parseBoolean(properties.getProperty("soundNotification", String.valueOf(DEFAULT_SOUND_NOTIFICATION)));
    }
    
    /**
     * 设置声音提醒
     */
    public void setSoundNotification(boolean soundNotification) {
        properties.setProperty("soundNotification", String.valueOf(soundNotification));
    }
    
    /**
     * 获取应用版本
     */
    public String getAppVersion() {
        return APP_VERSION;
    }
} 